<?php
class AgentAction extends CommonAction {
	function index(){
		$this->dao = D('Site');
		$this->listmap = "price <> ''";
        parent::_list();
		$list = $this->get('list');
		$agpricedb = D('Agentprice');
		$productdb = M('Product');
		foreach($list as $k => $v){
			$product = $productdb -> where(array('systemagent' => $v['id'], 'site_id' => $this->siteid)) -> find();
			if($product){
			    $list[$k]['product_id'] = $product['id'];
			}
			$list[$k]['price'] = json_decode($agpricedb -> getsaleprice($v['id']),true);
		}
		
		$this->assign('list', $list);
		$this->display();
	}
	
	//上架
	public function setProduct(){
		$id = $this->_get('id');
		$site = M('Site') -> where(array('id' => array('in', $id), 'price' => array('neq', ''))) -> select();
		if(!$site){
			$this->error('当前产品无法设定代理！可以是平台不允许。');exit;
		}
		$agpricedb = D('Agentprice');
		$productdb = M('Product');
	    $count = 0;
		foreach($site as $k => $v){
			if($productdb -> where(array('systemagent' => $v['id'], 'site_id' => $this->siteid)) -> find()){
				continue;
			}
			$data['title'] = $v['title'];
			$data['site_id'] = $this->siteid;
			$data['systemagentprice'] = $agpricedb -> getsaleprice($id);
			$data['systemagent'] = $id;
			$data['img_url'] = $v['img_url'];
			
			$ret = $productdb -> data($data) -> add();
			if($ret){
				$count ++;
			}
		}
		
		$this->success('成功上架'.$count.'个商品');
	}
}